<?php
class MainAction extends Action{
	Public function _initialize(){
		if(!session("?username")){
			$this->error("登录超时","/admin/");
		}
	}
	
	public function index(){
		$db=M('Class');
		$class=$db->select();
		$this->assign('class',$class);
		$this->display();
	}
	
	//用户提交资料列表
	public function jlist(){
		$table='Grj';
		$jtype=1;
		$model='grjlist';
		if($_REQUEST['jtype']==2){
			$table='Zzj';
			$jtype=2;
			$model='zzjlist';
		}else if($_REQUEST['jtype']==3){
			$table='Xmj';
			$jtype=3;
			$model='xmjlist';
		}
		if($this->_get("sblb")==1){
			$where['SBLB']='社会';
		}else if($this->_get("sblb")==2){
			$where['SBLB']='组织';
		}
		if(session("level")==1){
			$where['PROVINCE']=session("province");
		}else if($this->_get('province')!=null&&$this->_get('province')!='全部'){
			$where['PROVINCE']=$this->_get('province');
			$province=$this->_get('province');
		}
		import("ORG.Util.Page");// 导入分页类
		$grj=M($table);
		$grjcount=$grj->where($where)->count();//计算总条数
		$grjPage=new Page($grjcount,15);//实例化分页类 传入总记录数和每页显示的记录数
		$grjPage->parameter.="&sblb=".$this->_get("sblb")."&province=".$this->_get('province');
		$grjshow=$grjPage->show();//分页显示输出
		$grjList=$grj->where($where)->order("sort desc,id asc")->limit($grjPage->firstRow.','.$grjPage->listRows)->select();
		$provinceRs=M($table);
		$proList=$provinceRs->Distinct(true)->field('PROVINCE')->where("PROVINCE<>''")->select();
		$this->assign('province',$province);
		$this->assign('proList',$proList);
		$this->assign('jPage',$grjshow);//赋值分页输出
		$this->assign("jList",$grjList);
		$this->assign('jtype',$jtype);
		$this->assign("level",1);
		$this->display($model);
	}
	
	//省级提交资料列表
	public function votelist(){
		$table='Grj';
		$jtype=1;
		$model='grjlist';
		$where['zyz_vote.level']=1;
		if($_REQUEST['jtype']==2){
			$table='Zzj';
			$jtype=2;
			$model='zzjlist';
			$where['zyz_vote.level']=2;
		}else if($_REQUEST['jtype']==3){
			$table='Xmj';
			$jtype=3;
			$model='xmjlist';
			$where['zyz_vote.level']=3;
		}
		if($this->_get("sblb")==1){
			$where['zyz_'.$table.'.SBLB']='社会';
		}else if($this->_get("sblb")==2){
			$where['zyz_'.$table.'.SBLB']='组织';
		}
		if($this->_get('province')!=null&&$this->_get('province')!='全部'){
			$where['zyz_'.$table.'.PROVINCE']=$this->_get('province');
			$province=$this->_get('province');
		}
		import("ORG.Util.Page");// 导入分页类
		$vote=M('Vote');
		$where['zyz_vote.jtype']=1;
		$votecount=$vote->where($where)->join("zyz_".$table." on zyz_vote.jid=zyz_".$table.".id")->count();//计算总条数
		$votePage=new Page($votecount,15);//实例化分页类 传入总记录数和每页显示的记录数
		$votePage->parameter.="&sblb=".$this->_get("sblb")."&province=".$this->_get('province');
		$voteshow=$votePage->show();//分页显示输出
		$voteList=$vote->where($where)->join("zyz_".$table." on zyz_vote.jid=zyz_".$table.".id")->order("sort desc,zyz_".$table.".id asc")->limit($votePage->firstRow.','.$votePage->listRows)->select();
		$provinceRs=M($table);
		$proList=$provinceRs->Distinct(true)->field('PROVINCE')->where("PROVINCE<>''")->select();
		$this->assign('province',$province);
		$this->assign('proList',$proList);
		$this->assign('jPage',$voteshow);//赋值分页输出
		$this->assign("jList",$voteList);
		$this->assign('jtype',$jtype);
		$this->assign("level",2);
		$this->display($model);
	}
	
	
	//上报
	public function insert(){
		$vote=M('Vote');
		$jids=$_POST["jid"];
		$success=0;//成功条数
		$error=0;//失败条数
		$exists=0;//存在条数
		$level=session("level");
		$jtype=$this->_post("jtype");
		$len=0;
		if($level=='1'){
			if($jtype=='1'){
				$len=10;
			}else if($jtype=='2'){
				$len=5;
			}else if($jtype=='3'){
				$len=5;
			}
		}else if($level=='2'){
			if($jtype=='1'){
				$len=6;
			}else if($jtype=='2'){
				$len=4;
			}else if($jtype=='3'){
				$len=4;
			}
		}
		if($len!=count($jids)){
			$this->error('上报数量不正确，您勾选了'.count($jids).'调条数据，应该勾选'.$len.'条数据');
		}else{
			foreach($jids as $j){
				$where['jid']=$j;
				$where['jtype']=$this->_post("jtype");
				$UserExists=$vote->where($where)->select();
				if(!$UserExists){
					$data['jid']=$j;
					$data['province']=session('province');
					$data['ctime']=Date("Y-m-d H:i:s",time());
					$data['jtype']=$this->_post("jtype");
					$data['level']=session("level");
					$vote->startTrans();
					if(!$vote->data($data)->add()){
						$vote->rollback();
						$error++;
					}else{
						$vote->commit();
						$success++;
					}
				}else{
					$exists++;
				}
			}
			$this->success("上报状态：成功[".$success."]条，跳过已存在[".$exists."]条，失败[".$error."]条");
		}
	}
	
	//排序
	function sort(){
		$tab=M();
		if($this->_get("jtype")=="1"){
			$tab=M("grj");
		}else if($this->_get("jtype")=="2"){
			$tab=M("zzj");
		}else if($this->_get("jtype")=="3"){
			$tab=M("xmj");
		}
		$where['id']=$this->_get("id");
		$tab->where($where)->setInc("sort");
		$this->success('操作成功');
	}
	
	//打开更新密码页面
	public function updatepwd(){
		$this->display();
	}
	
	//更新密码
	public function doupdatepwd(){
		$admin=M("Adm");
		$where['uid']=session("uid");
		$where['password']=$this->_post("oldpassword");
		$adminValid=$admin->where($where)->select();
		if(!empty($adminValid)){
			$data['password']=$this->_post("password");
			$where_u['id']=$adminValid[0]['id'];
			if($admin->where($where_u)->save($data)){
				$this->success("更新成功");
			}else{
				$this->error("旧密码不能与新密码相同");
			}
		}else{
			$this->error("旧密码不正确，请重新输入");
		}
	}
	
	//显示详情
	public function showinfo(){
		if($this->_get("jtype")=="1"){
			$info=M("grj");
			$page='grjinfo';
		}else if($this->_get("jtype")=="2"){
			$info=M("zzj");
			$page='zzjinfo';
		}else if($this->_get("jtype")=="3"){
			$info=M("xmj");
			$page='xmjinfo';
		}
		$where['id']=$this->_get("id");
		$infolist=$info->where($where)->select();
		$this->assign('info',$infolist);
		$this->display($page);
	}
	
	//打开配置页面
	public function config(){
		$table=M('Config');
		$config=$table->select(1);
		$this->assign('config',$config[0]);
		$this->display();
	}
	
	//更新配置
	public function configupdate(){
		$table=M('Config');
		$data['showpage']=$this->_post('showpage');
		$data['level']=$this->_post('level');
		$where['id']=1;
		if($table->data($data)->where($where)->save()){
			$this->success('保存成功');
		}else{
			$this->error('保存失败');
		}
		
	}
}